﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Reflection;

namespace Fulcrum
{
    class CommandLineSetting
    {
        PropertyInfo property;
        ISettings settings;

        public CommandLineSetting(PropertyInfo property, ISettings settings, SettingAttribute settingsAttrib)
        {
            this.property = property;
            this.settings = settings;

            this.Optional = settingsAttrib.Optional;
            this.Description = settingsAttrib.Description;

            string name = CalculateName(settingsAttrib);
            this.Name = name;
        }

        private string CalculateName(SettingAttribute settingsAttrib)
        {
            string name = settingsAttrib.Name;

            if (String.IsNullOrEmpty(name))
            {
                name = this.property.Name;
            }

            return name;
        }

        public string Name
        {
            get;
            private set;
        }

        public string Description
        {
            get;
            private set;
        }

        public bool Optional
        {
            get;
            private set;
        }

        public void SetValue(string value)
        {
            object realValue = ValueConverter.ConvertStringToObject(value, this.property.PropertyType);
            this.property.SetValue(this.settings, realValue, null);

            this.HasValue = true;
        }

        public bool HasValue
        {
            get;
            private set;
        }
    }

}
